﻿/*-------------------------------------------------------------------------
* 命名空间: WaferSelectV3.CustomControls/CustomScrollLabel
* 类       名: CustomScrollLabel
* 功      能:  自定义Label,实现走马灯的效果
* 时      间:  2024-05-21 16:15:32
* 版      本:  V1.0
* 作      者:  Fioman
* 格      言:  Talk is cheap,show me the code ^_^
*-------------------------------------------------------------------------*/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
using System.Windows;
using System.Windows.Automation.Peers;
using System.Windows.Controls;
using System.Windows.Media;
using System.Windows.Media.Animation;

namespace WaferSelectV3.CustomControls
{
    public class CustomScrollLabel : Label
    {
        private double _runSpeed = 800; // 走马灯的速度
        private string? _textSource;
        /// <summary>
        /// 显示的文字源
        /// </summary>
        public string? TextSource
        {
            get { return _textSource; }
            set { _textSource = value; }
        }

        private string? _textShow;
        /// <summary>
        /// 界面上要显示的文字
        /// </summary>
        public string? TextShow
        {
            get { return _textShow; }
            set
            {
                _textShow = value;
            }
        }

        /// <summary>
        /// 设置界面显示内容
        /// </summary>
        /// <param name="value"></param>
        private void SetContent(string value)
        {
            Content = value;
        }

        /// <summary>
        /// 文字显示开始时间
        /// </summary>
        private DateTime? _startTime;

        private string? _textTemp;
        /// <summary>
        /// 过渡文字
        /// </summary>
        public string? TextTemp
        {
            get { return _textTemp; }
            set { _textTemp = value; }
        }

        /// <summary>
        /// 要显示的字符串长度,如果超过了这个长度,就用走马灯来显示
        /// </summary>
        public int TextShowLength = 12;
        public int TextTotalLength;
        public int StartIndex;
        public CustomScrollLabel()
        {
            Loaded += CustomScrollLabel_Loaded;
        }

        private void CustomScrollLabel_Loaded(object sender, RoutedEventArgs e)
        {
            if (Content != null && Content.ToString()!.Length > TextShowLength)
            {
                TextSource = Content.ToString();
                // 只显示前面的12个字符
                StartIndex = 0;
                TextTemp = new string(' ', 3) + TextSource + new string(' ', 3);
                TextShow = TextTemp!.Substring(StartIndex, TextShowLength);
                TextTotalLength = TextTemp!.Length;
                Timer runTimer = new Timer();
                runTimer.Interval = _runSpeed;
                runTimer.Enabled = true;
                runTimer.Elapsed += RunTimer_Elapsed;
            }
        }

        private async void RunTimer_Elapsed(object? sender, ElapsedEventArgs e)
        {
            if (string.IsNullOrEmpty(TextShow))
                return;
            if ((StartIndex + TextShowLength) == TextTotalLength)
            {
                StartIndex = 0;
                TextShow = TextTemp!.Substring(StartIndex, TextShowLength);
                await Task.Delay(500);
            }
            else
            {
                StartIndex++;
                TextShow = TextTemp!.Substring(StartIndex, TextShowLength);
            }
            Application.Current?.Dispatcher?.Invoke(() =>
            {
                SetContent(TextShow);
            });
        }
    }
}
